package com.yh.rent.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.yh.rent.dao.UserDao;
import com.yh.rent.entity.User;
import com.yh.rent.utils.HibernateUtil;

public class UserDaoImpl implements UserDao {
	Session session = null ;
	@Override
	public List<User> getAllUsers() {
		session = HibernateUtil.getCurrentSession() ;
		Transaction ts = session.beginTransaction() ;
		Query query = session.createQuery("from User") ;
		@SuppressWarnings("unchecked")
		List<User> list = query.list() ;
		ts.commit(); 
		return list;
	}

	@Override
	public User getUserById(Integer id) {
		session = HibernateUtil.getCurrentSession() ;
		Transaction ts = session.beginTransaction() ;
		Query query = session.createQuery("from User where id =:id") ;
		query.setParameter("id", id) ;
		User user = (User)query.uniqueResult() ;
		ts.commit();
		return user;
	}

	@Override
	public int addUser(User user) {
		session = HibernateUtil.getCurrentSession() ;
		Transaction ts = session.beginTransaction() ;
		int k = (Integer)session.save(user) ;
		ts.commit(); 
		return k ;
	}

}
